<template>
  <div class="WorkingSchedule" v-loading="loading">
    <el-form ref="form" :model="form" label-position="top" class="myform">
      <div>
        <el-form-item label="发起人" class="myform-item">
          <el-input
            v-model="form.sponsor"
            placeholder="请输入发起人"
            @keyup.enter.native="onSearch"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="申请日期">
          <el-date-picker
            value-format="yyyy-MM-dd"
            v-model="date"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
      </div>
      <div>
        <el-form-item style="margin-top: 50px">
          <el-button @click="onSearch">查询</el-button>
          <el-button type="primary" @click="onRouterOne" :disabled="!qxobj.add">采购录入</el-button>
        </el-form-item>
      </div>
    </el-form>
    <el-table :data="tableData" style="width: 100%">
      <template v-for="(item, index) in tableHeader">
        <el-table-column
          :label="item.name"
          :key="index"
          align="center"
          :prop="item.key"
        >
        </el-table-column>
      </template>
      <el-table-column label="操作" align="center" width="300">
        <template slot-scope="{ row }">
          <el-button type="primary" size="small" @click="onRouter(row.id)" :disabled="!qxobj.save"
            >修改</el-button
          >
          <el-button type="danger" size="small" @click="onDelete(row.id)" :disabled="!qxobj.del"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :form="form"
      :total="total"
      @paginationPostOrderTaskList="postApiadministrationindex"
    />
  </div>
</template>

<script>
import { postApiadministrationindex, postApiadministrationdel } from "@/api";
import pagination from "@/components/pagination.vue";
export default {
  components: { pagination },
  data() {
    return {
      qxobj:{},
      loading: false,
      date: "",
      form: {
        sponsor: "",
        strtime: "",
        endtime: "",
        page: 1,
        size: 10,
      },
      tableHeader: [
        {
          name: "部门",
          key: "department",
        },
        {
          name: "发起人",
          key: "sponsor",
        },
        {
          name: "物料名称",
          key: "material",
        },
        {
          name: "数量",
          key: "num",
        },
        {
          name: "单价",
          key: "price",
        },
        {
          name: "总金额",
          key: "amount",
        },
        {
          name: "申请日期",
          key: "launch_date",
        },
        {
          name: "采购日期",
          key: "purchase_date",
        },
      ],
      tableData: [],
      total: 1,
    };
  },
  activated() {
    this.qxobj = JSON.parse(this.$route.query.qxobj)
    let form = this.form;
    this.postApiadministrationindex(form);
  },
  methods: {
    // 录入
    onRouter(id) {
      this.$router.push({ name: "Procurement", query: { id } });
    },
    onRouterOne() {
      this.$router.push("/Procurement");
    },
    // 删除
    onDelete(id) {
      this.$confirm("此操作将永久删除该采购信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          postApiadministrationdel(id)
            .then((res) => {
              this.$message.success("删除成功");
              this.postApiadministrationindex(this.form);
            })
            .catch((err) => {
              this.$message.warning(err.msg);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 编辑
    onEdit(e) {
      console.log("编辑", e);
      this.detail = e;
      this.dialogVisible = true;
    },
    // 采购列表
    async postApiadministrationindex(data) {
      this.form = data;
      postApiadministrationindex(data).then((res) => {
        console.log("采购列表", res);
        this.tableData = res.data;
        this.total = Number(res.total);
      });
    },
    // 搜索
    onSearch() {
      let date = this.date;
      if (date) {
        this.form.strtime = date[0];
        this.form.endtime = date[1];
      }
      this.form.page = 1 
      this.form.size = 10
      this.postApiadministrationindex(this.form);
    },
  },
};
</script>

<style lang="scss" scoped>
.myform {
  display: flex;
  justify-content: space-between;
  > div {
    display: flex;
  }
  &-item {
    margin-right: 10px;
  }
}
.WorkingSchedule {
  background: #ffff;
  padding: 20px;
  border-radius: 2px;
  @include boxShadow;
}
</style>
